//
//  VideoDetailController.m
//  ZeroCorner
//
//  Created by lanou3g on 15/11/20.
//  Copyright © 2015年 Zero_Zhou. All rights reserved.
//

#import "VideoDetailController.h"
#import <AVFoundation/AVFoundation.h>
#import <UIImageView+WebCache.h>

#define FrameW self.view.frame.size.width
#define FrameH self.view.frame.size.height

@interface VideoDetailController ()
@property (nonatomic,strong)UIImageView *smallImageView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UILabel *dateLabel;
@property (nonatomic,strong)UILabel *vcntNiceLabel;
@property (nonatomic,strong)UIButton *collectionButton;
@property (nonatomic,strong)UILabel *contentLabel;
@property (nonatomic,strong)UIButton *downloadButton;
@property (nonatomic,strong)UIButton *forwardingButton;//转发按钮
@property (nonatomic,strong)AVPlayer *player;
@property (nonatomic,strong)AVPlayerLayer *playerlayer;

@end


@implementation VideoDetailController
- (void)viewDidLoad{
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xiala"] style:UIBarButtonItemStylePlain target:self action:@selector(addcell:)];
    
    
    [self setSubViews];
    [self setCornerImageView];
    [self layoutModels];
    [self setVideo];
    
    //[self.smallImageView sd_setImageWithURL:[NSURL URLWithString:self.videoModel.own[@"icon"]]];
    
}

- (void)addcell:(UIBarButtonItem *)rightBarButtonItem{
    
    
    
}
- (void)setSubViews{
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 74, FrameW / 2, 20)];
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collectionButton.frame = CGRectMake(280, 79, FrameW / 5, 30);
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 94, FrameW / 5, 20)];
    self.vcntNiceLabel = [[UILabel alloc] initWithFrame:CGRectMake(65 + FrameW / 5, 94, FrameW / 4, 20)];
    
    //self.nameLabel.backgroundColor = [UIColor redColor];
    self.collectionButton.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.collectionButton];
    [self.view addSubview:self.dateLabel];
    [self.view addSubview:self.vcntNiceLabel];
    
}
- (void)setCornerImageView{
    self.smallImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 74, 40, 40)];
    [self.smallImageView.layer setMasksToBounds:YES];
    [self.smallImageView.layer setCornerRadius:self.smallImageView.frame.size.height / 2.0];
    self.smallImageView.layer.borderWidth = 1;
    [self.smallImageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
    self.smallImageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.smallImageView];
}
- (void)layoutModels{
    [self.smallImageView sd_setImageWithURL:[NSURL URLWithString:self.videoModel.own[@"icon"]]];
    self.nameLabel.text = self.videoModel.own[@"nick"];
    [self.collectionButton setTitle:@"+ 收藏" forState:UIControlStateNormal];
    self.dateLabel.font = [UIFont systemFontOfSize:14];
    self.dateLabel.text = self.videoModel.ext2[@"createTimeNice"];
    
    //带属性的字符串
    NSString *vcntText = [NSString stringWithFormat:@"%@  热度",self.videoModel.stat[@"vcntNice"]];
    //创建可变额属性字符串
    NSMutableAttributedString *vcntAttributedText = [[NSMutableAttributedString alloc] initWithString:vcntText];
    [vcntAttributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 5)];
    self.vcntNiceLabel.font = [UIFont systemFontOfSize:14];
    self.vcntNiceLabel.attributedText = vcntAttributedText;
    
}
- (void)setVideo{
    NSString *str= [NSString stringWithFormat:@"%@%@.%@",self.videoModel.stream[@"base"],self.videoModel.scid,self.videoModel.stream[@"ios"]];
    NSURL *url = [NSURL URLWithString:str];
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    self.player = [AVPlayer playerWithPlayerItem:item];
    self.playerlayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerlayer.videoGravity = AVLayerVideoGravityResizeAspect;
    self.playerlayer.frame = CGRectMake(0, 120, FrameW, FrameH - 300);
    self.playerlayer.backgroundColor = [[UIColor blackColor]CGColor];
    [self.view.layer addSublayer:self.playerlayer];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [self.player play];
//    [self.playerlayer removeFromSuperlayer];
}
- (void)setVideoModel:(VideoModel *)videoModel{
    if (_videoModel != videoModel) {
        _videoModel = nil;
        _videoModel = videoModel;
    }
}

@end
